package com.group.controller;

import com.base.view.GeneralResult;
import com.group.model.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.base.view.BaseResult;
import com.base.view.ListResult;
import com.group.interface1.RequireSuperAdmin;
import com.group.util.NoteException;


/**
 * 后台支付管理系统
 * 
 * @author zhaolei
 *
 */
@Controller
public class BackPaymentController extends BaseController {
	private static final org.slf4j.Logger logger = LoggerFactory.getLogger(BackPaymentController.class);

	/*@GetMapping("/back/payment/log")
	@ResponseBody
	public ListResult<UserTickerLog> queryUserTickerLog(Integer start, Integer limit, Long userId) {
		validPage(start, limit);
		return new ListResult<UserTickerLog>().setList(tickerDao.queryBackUserTickerLog("提现", userId, start, limit)).setTotal(tickerDao.queryBackUserTickerLogCount("提现", userId));
	}*/

	/**
	 * 用户提现日志
	 * @param start 开始
	 * @param limit 条数
	 * @param userId 用户id
	 * @param reason 原因
	 * @param userName 用户名称
	 * @param date 日期
	 * @return UserTickerLogList
	 */
	@GetMapping("/back/payment/log")
	@ResponseBody
	public ListResult<UserTickerLog> queryUserTickerLog(Integer start, Integer limit, Long userId,String reason,String userName,String date) {
		validPage(start, limit);
		if(reason==null){
			return null;
		}else if(reason.equals("提现")){
			return new ListResult<UserTickerLog>().setList(tickerDao.queryBackUserTickerLog(reason, userId, start, limit,userName,date)).setTotal(tickerDao.queryBackUserTickerLogCount(reason, userId,userName,date));
		}else{
			return null;
		}
	}

	/**
	 * 用户币币转账日志
	 * @param start 开始
	 * @param limit 条数
	 * @param userId 用户id
	 * @param userName 用户名称
	 * @param date 日期
	 * @return CoinChargeInfo List
	 */
	@GetMapping("/back/bbcharge/log")
	@ResponseBody
	public ListResult<CoinChargeInfo> queryUserBbChargeLog(Integer start, Integer limit, Long userId,String userName,String date) {
		validPage(start, limit);
		return new ListResult<CoinChargeInfo>().setList(tickerDao.queryBackUserBbLog(userId, start, limit,userName,date)).setTotal(tickerDao.queryBackUseriBbLogCount(userId,userName,date));
	}


	/**
	 * 统计wecc总额
	 * @param date 日期
	 * @param userId 用户id
	 * @param userName 用户名称
	 * @param type 类型
	 * @return count
	 */
	@GetMapping("/back/payment/account")
	@ResponseBody
	public GeneralResult<Count> queryAccount(String date,Long userId,String userName,String type) {
		Count count = new Count();
		//wecc总余额
		count.setNum(tickerDao.queryNumCount());

		//wecc总投资额
		if(type==null || type.equals("")){
			count.setAmount(tickerDao.queryAmountCount(date,userId));
		}else{
			count.setAmount(tickerDao.queryAmountCountType(date,userId,Integer.parseInt(type)));
		}

		//wecc总充值金额
		count.setRecharge(tickerDao.queryRechargeCount(date));

		//wecc总充值笔数
		count.setRechargeAction(tickerDao.queryRechargeCountAction(date));

		//wecc总币币充值笔数
		count.setBbrechargeAction(tickerDao.queryBbRechargeCountAction(date));

		//wecc总币币充值金额
		count.setBbrecharge(tickerDao.queryBbRechargeCount(date));

		//wecc总收益额
		if(type==null || type.equals("")){
			count.setBonus(tickerDao.queryBonusCount(date,userId));
		}else{
			count.setBonus(tickerDao.queryBonusCountType(date,userId,Integer.parseInt(type)));
		}

		//总用户数
		count.setPeopleNum(tickerDao.queryPeopleNumCount());

		//小说数量
		count.setNovelNum(tickerDao.queryNovelNumCount(0));

		//漫画数量
		count.setCartoonNum(tickerDao.queryNovelNumCount(1));

		//wecc总充值rmb金额
		count.setRmb(tickerDao.queryRechargeRmbCount(date,userId,userName));

		//wecc已提现总量
		if(tickerDao.queryReated(date,userId)==null){
			count.setTreated(0.0);
		}else{
			count.setTreated(-tickerDao.queryReated(date,userId));
		}

		//wecc未处理总量
		if(tickerDao.queryUnReated(date,userId)==null){
			count.setUntreated(0.0);
		}else{
			count.setUntreated(-tickerDao.queryUnReated(date,userId));
		}

		return new GeneralResult<Count>().setValue(count);
	}

	/**
	 * 用户微信充值日志
	 * @param start 开始
	 * @param limit	条数
	 * @param userId 用户id
	 * @param name name
	 * @param date date
	 * @param type type
	 * @return 订单号列表
	 */
	@GetMapping("/exchange/pay/querydetail")
	@ResponseBody
	public ListResult<OutTradeNoLog> queryOutTradeNoLogDetails(Integer start, Integer limit, Long userId, String name, String date, String type) {
		validPage(start, limit);
		return new ListResult<OutTradeNoLog>().setList(tickerDao.queryOutTradeNoLogDetail(userId, start, limit,name,date,type)).
				setTotal(tickerDao.queryOutTradeNoLogDetailCount(userId,name,date,type));
	}

	/**
	 * 批量确认提现成功
	 * 
	 * @param userTickerLogIds, 确认提现的id 逗号分隔
	 *
	 * @return baseResult
	 */
	@GetMapping("/back/payment/confirm")
	@ResponseBody
	// @RequireSuperAdmin
	public synchronized BaseResult confirm(String userTickerLogIds) {
		if (userTickerLogIds == null) {
			throw new NoteException("userTickerLogIds 不能为空");
		}

		for (String idstr : userTickerLogIds.split(",")) {
			Long id = Long.parseLong(idstr);

			UserTickerLog log = beanDao.quickQueryOneById(UserTickerLog.class, id);
			if (log == null) {
				throw new NoteException("id = " + id + " 的提现日志不存在");
			}

			if (!log.getState().equals("进行中"))
				throw new NoteException("只能处理状态为进行中的");

			if (!log.getCid().equals("wecc"))
				throw new NoteException("只能处理wecc提现");

			User user = beanDao.quickQueryOneById(User.class, log.getUserId());

			UserTicker ut = tickerService.getUserTicker(log.getUserId(), log.getCid());

			// 更新提现中金额
			ut.setCashing(0d);
			beanDao.save(ut);

			// 提现金额
			Double cash = log.getAbsNum();
			if (log.getFee() != null)
				cash = cash - log.getFee();

			// 更新日志状态
			log.setState("已完成");
			beanDao.save(log);
		}

		return new BaseResult();
	}

	/**
	 * 取消进行中的提现
	 * 
	 * @param userTickerLogId  钱包日志id
	 * @return baseResult
	 */
	@GetMapping("/back/payment/calcel")
	@ResponseBody
	@RequireSuperAdmin
	public BaseResult cancelCash(Long userTickerLogId) {
		UserTickerLog log = beanDao.quickQueryOneById(UserTickerLog.class, userTickerLogId);

		if (log == null || !"进行中".equals(log.getState())) {
			throw new NoteException("只能取消进行中的提现");
		}

		log.setState("已取消");
		beanDao.save(log);

		// 回退用户账户
		tickerService.addTicker(log.getUserId(), log.getCid(), -log.getNum(), "用户提现取消", null, "已完成", log.getBillNo(), null, null);

		// 设置用户提现中的金额为0
		UserTicker ut = tickerService.getUserTicker(log.getUserId(), log.getCid());
		ut.setCashing(0d);
		beanDao.save(ut);

		return new BaseResult();
	}

	/**
	 * 提现记录取消,钱转入竞猜账户
	 * 
	 * @param userTickerLogId 钱包日志id
	 * @return baseRsult
	 */
	@GetMapping("/back/payment/refund")
	@ResponseBody
	public BaseResult refundCash(Long userTickerLogId) {
		UserTickerLog log = beanDao.quickQueryOneById(UserTickerLog.class, userTickerLogId);

		if (log == null)
			throw new NoteException("日志不存在");

		if (!log.getState().equals("进行中"))
			throw new NoteException("日志状态不是进行中");

		// 提现中金额为0
		UserTicker ut = tickerService.getUserTicker(log.getUserId(), "wecc");
		ut.setCashing(0d);
		beanDao.save(ut);

		// 增加竞猜币
		CasinoInfo info = beanDao.quickQueryOne(CasinoInfo.class, "userId", log.getUserId());
		info.setNum(info.getNum() - log.getNum());
		beanDao.save(info);

		// 修改日志状态
		log.setState("退回到竞猜余额");
		beanDao.save(log);
		return new BaseResult().setMessage("回滚成功");

	}

}
